//
//  NewAddPrivateViewController.m
//  fitnessmanage
//
//  Created by wujian on 2018/1/24.
//  Copyright © 2018年 zongquankeji. All rights reserved.
//

#import "NewAddPrivateViewController.h"
#import "AddPrivateTeachView.h"
#import "MOFSPickerManager.h"
#import "HDAlertView.h"
#import "TeachPrivateClassViewController.h"

@interface NewAddPrivateViewController ()

@property(nonatomic,weak)AddPrivateTeachView *addPrivateTeachView;

@property(nonatomic,strong)NSMutableDictionary *dataDict;

@end

@implementation NewAddPrivateViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title = @"新私教课排期";
    
    self.view.backgroundColor = BackGroundColor;
    
    [self createSubView];
    
    NSString *courseIds = @"";
    
    for (NSString *ids in self.courseArray) {
        
        if ([ids isEqual:[self.courseArray lastObject]]) {
            
            courseIds = [NSString stringWithFormat:@"%@%@",courseIds,ids];
        }
        else{
            
            courseIds = [NSString stringWithFormat:@"%@%@,",courseIds,ids];
        }
    }
    
    [self.dataDict setObject:courseIds forKey:@"CourseIDs"];
    
    [self.dataDict setObject:self.workId forKey:@"TeacherID"];
}

#pragma mark 创建子视图
-(void)createSubView{
    
    AddPrivateTeachView *addPrivateTeachView = [[AddPrivateTeachView alloc] init];
    
    addPrivateTeachView.beginCourseTimeView.choseBtn.tag = 0;
    [addPrivateTeachView.beginCourseTimeView.choseBtn addTarget:self action:@selector(choseDateWithBtn:)];
    
    addPrivateTeachView.endCourseTimeView.choseBtn.tag = 1;
    [addPrivateTeachView.endCourseTimeView.choseBtn addTarget:self action:@selector(choseDateWithBtn:)];
    
    
    addPrivateTeachView.appointBeginTimeView.choseBtn.tag = 0;
    [addPrivateTeachView.appointBeginTimeView.choseBtn addTarget:self action:@selector(choseTime:)];
    
    addPrivateTeachView.appointEndTimeView.choseBtn.tag = 1;
    [addPrivateTeachView.appointEndTimeView.choseBtn addTarget:self action:@selector(choseTime:)];
    
    
    //约课间隔时间
    [addPrivateTeachView.appointLimitView.choseBtn addTarget:self action:@selector(appointLimitBtnClick)];
    
    //预约开放
    [addPrivateTeachView.openView.choseBtn addTarget:self action:@selector(pointBtnClick)];
    
    [addPrivateTeachView.sureBtn addTarget:self action:@selector(sureBtnClick)];
    
    [self.view addSubview:addPrivateTeachView];
    
    self.addPrivateTeachView = addPrivateTeachView;
    
    [addPrivateTeachView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self.view);
    }];
}


#pragma mark 开始日期选择
-(void)choseDateWithBtn:(UIButton *)btn{
    
    [self.view endEditing:YES];
    
    __weak typeof(self)weakSelf = self;
    
    [[MOFSPickerManager shareManger] showDatePickerWithTag:132 title:@"选择时间" cancelTitle:@"取消" commitTitle:@"确定" datePickerMode:UIDatePickerModeDate commitBlock:^(NSDate *date) {
        
        NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];//创建一个日期格式化器
        
        dateFormatter.dateFormat=@"yyyy-MM-dd";//指定转date得日期格式化形式
        
        NSString *formatDate = [dateFormatter stringFromDate:date];
        
        if (btn.tag == 0) {
            
            if ([weakSelf compareOneDay:formatDate withAnotherDay:weakSelf.addPrivateTeachView.endCourseTimeView.choseLabel.text] > 0) {
                
                [MBProgressHUD showText:@"开始日期不能大于结束日期"];
                
                return ;
            }
            
            weakSelf.addPrivateTeachView.beginCourseTimeView.choseLabel.text = formatDate;
            
            weakSelf.addPrivateTeachView.beginCourseTimeView.choseLabel.textColor = TFColor;
            
            [weakSelf.dataDict setObject:formatDate forKey:@"BeginTime"];
            
        }
        else{
            
            if ([weakSelf compareOneDay:formatDate withAnotherDay:weakSelf.addPrivateTeachView.endCourseTimeView.choseLabel.text] < 0) {
                
                [MBProgressHUD showText:@"结束日期不能小于开始日期"];
                
                return ;
            }
            
            weakSelf.addPrivateTeachView.endCourseTimeView.choseLabel.text = formatDate;
            weakSelf.addPrivateTeachView.endCourseTimeView.choseLabel.textColor = TFColor;
            [weakSelf.dataDict setObject:formatDate forKey:@"EndTime"];
        }
        
    } cancelBlock:^{
        
    }];
}

#pragma mark 选择时间
-(void)choseTime:(UIButton *)btn{
    
    [self.view endEditing:YES];
    
    __weak typeof(self)weakSelf = self;
    
    [[MOFSPickerManager shareManger] showDatePickerWithTag:133 title:@"选择时间" cancelTitle:@"取消" commitTitle:@"确定" datePickerMode:UIDatePickerModeTime commitBlock:^(NSDate *date) {
        
        NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];//创建一个日期格式化器
        
        dateFormatter.dateFormat=@"HH:mm";//指定转date得日期格式化形式
        
        NSString *formatDate = [dateFormatter stringFromDate:date];                
    
        if (btn.tag == 0) {
            
            if ([weakSelf compareOneDay:formatDate withAnotherDay:weakSelf.addPrivateTeachView.appointEndTimeView.choseLabel.text] > 0) {
                
                [MBProgressHUD showText:@"开始日期不能大于结束日期"];
                
                return ;
            }
            
            weakSelf.addPrivateTeachView.appointBeginTimeView.choseLabel.text = formatDate;
            weakSelf.addPrivateTeachView.appointBeginTimeView.choseLabel.textColor = TFColor;
            [weakSelf.dataDict setObject:formatDate forKey:@"BeginHour"];
        }
        else{
            
            if ([weakSelf compareOneDay:formatDate withAnotherDay:weakSelf.addPrivateTeachView.appointEndTimeView.choseLabel.text] < 0) {
                
                [MBProgressHUD showText:@"结束日期不能小于开始日期"];
                
                return ;
            }
            
            weakSelf.addPrivateTeachView.appointEndTimeView.choseLabel.text = formatDate;
            weakSelf.addPrivateTeachView.appointEndTimeView.choseLabel.textColor = TFColor;
            [weakSelf.dataDict setObject:formatDate forKey:@"EndHour"];
        }
        
    } cancelBlock:^{
        
    }];
}

#pragma mark 预约开发
-(void)pointBtnClick{
    
    __weak typeof(self)weakSelf = self;
    
    HDAlertView *sheetView = [HDAlertView showActionSheetWithTitle:nil];
    
    sheetView.buttonsListStyle = HDAlertViewButtonsListStyleRows;
    
    
    [sheetView addButtonWithTitle:@"立即开放" type:HDAlertViewButtonTypeDefault handler:^(HDAlertView *alertView) {
        
        weakSelf.addPrivateTeachView.openView.choseLabel.text = @"立即开放";
        
        weakSelf.addPrivateTeachView.openView.choseLabel.textColor = TFColor;
        
        [weakSelf.dataDict setObject:@"1" forKey:@"OrderType"];
        
        [weakSelf.dataDict removeObjectForKey:@"OrderHour"];
    }];
    
    [sheetView addButtonWithTitle:@"定时开放" type:HDAlertViewButtonTypeDefault handler:^(HDAlertView *alertView) {
        
        [weakSelf.dataDict setObject:@"2" forKey:@"OrderType"];
        
        [weakSelf.dataDict removeObjectForKey:@"OrderHour"];
        
        [weakSelf timingTime];
    }];
    
    [sheetView addButtonWithTitle:@"课程前3小时开放" type:HDAlertViewButtonTypeDefault handler:^(HDAlertView *alertView) {
        
        weakSelf.addPrivateTeachView.openView.choseLabel.text = @"前3小时开放";
        
        weakSelf.addPrivateTeachView.openView.choseLabel.textColor = TFColor;
        
        [weakSelf.dataDict setObject:@"3" forKey:@"OrderType"];
        
        [weakSelf.dataDict setObject:@"3" forKey:@"OrderHour"];
    }];
    
    [sheetView addButtonWithTitle:@"确定" type:HDAlertViewButtonTypeDefault handler:^(HDAlertView *alertView) {
        
        
    }];
    
    [sheetView show];
}

/**
 定时开发选择时间
 */
-(void)timingTime{
    
    __weak typeof(self)weakSelf = self;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [[MOFSPickerManager shareManger] showDatePickerWithTag:1219 title:@"选择时间" cancelTitle:@"取消" commitTitle:@"确定" datePickerMode:UIDatePickerModeDateAndTime commitBlock:^(NSDate *date) {
            
            NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];//创建一个日期格式化器
            
            dateFormatter.dateFormat=@"yyyy-MM-dd HH:mm:ss";//指定转date得日期格式化形式
            
            weakSelf.addPrivateTeachView.openView.choseLabel.text = [dateFormatter stringFromDate:date];
            
            weakSelf.addPrivateTeachView.openView.choseLabel.textColor = TFColor;
            
            [weakSelf.dataDict setObject:[dateFormatter stringFromDate:date] forKey:@"OrderDateTime"];
            
        } cancelBlock:^{
            
            
        }];
        
    });
    
}

#pragma mark 间隔时间
-(void)appointLimitBtnClick{
    
    [self.view endEditing:YES];
    
    __weak typeof(self)weakSelf = self;
    
    NSMutableArray *timeArray = [[NSMutableArray alloc] init];
    
    for (int i = 15; i <= 120; i+=15) {
        
        [timeArray addObject:[NSString stringWithFormat:@"%d分钟",i]];
    }
    
    [[MOFSPickerManager shareManger] showPickerViewWithDataArray:timeArray tag:111 title:@"选择间隔时间" cancelTitle:@"取消" commitTitle:@"确定" commitBlock:^(NSString *string) {
        
        weakSelf.addPrivateTeachView.appointLimitView.choseLabel.text = string;
        
        weakSelf.addPrivateTeachView.appointLimitView.choseLabel.textColor = TFColor;
        
        NSString *time = [[string componentsSeparatedByString:@"分"] firstObject];
        
        [weakSelf.dataDict setObject:time forKey:@"TimeDiff"];
        
    } cancelBlock:^{
        
    }];
}


/**
 确定按钮点击
 */
-(void)sureBtnClick{
    
    if ([[self.dataDict objectForKey:@"TeacherID"] isEmptyString] ||
        [[self.dataDict objectForKey:@"OrderType"] isEmptyString] ||
        [[self.dataDict objectForKey:@"TimeDiff"] isEmptyString]  ||
        [[self.dataDict objectForKey:@"EndHour"] isEmptyString]  ||
        [[self.dataDict objectForKey:@"BeginHour"] isEmptyString]  ||
        [[self.dataDict objectForKey:@"BeginDate"] isEmptyString]  ||
        [[self.dataDict objectForKey:@"EndDate"] isEmptyString]   ) {
        
        [MBProgressHUD showText:@"请输入必填项"];
        
        return;
    }
    
    //单节可约人数
    NSString *Persons = self.addPrivateTeachView.cardCourseView.titleTF.text;
    
    //单节可约人数
    NSString *MinPersons = self.addPrivateTeachView.minCourseView.titleTF.text;
    
    if ([Persons isEmptyString] || [MinPersons isEmptyString]) {
        
        [MBProgressHUD showText:@"请输入单节可约人数和最小上课人数"];
        
        return;
    }
    
    [self.dataDict setObject:Persons forKey:@"Persons"];
    
    [self.dataDict setObject:MinPersons forKey:@"MinPersons"];
    
    [self.dataDict setObject:@"1" forKey:@"Type"];
        
    VenueModel *venueModel = [VenueModel venue];
    
    [self.dataDict setObject:venueModel.venueId forKey:@"ShopID"];
    
    [MBProgressHUD showMessage:@"上传中..." toView:self.view];
    
    [HTTPTool POSTRaw:Post_CoursePlanOperate parameters:self.dataDict success:^(id responseObject) {
        
        [MBProgressHUD hideHUDForView:self.view];
        
        if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0){
            
            [MBProgressHUD showText:@"操作成功"];
            
            for (UIViewController *temp in self.navigationController.viewControllers) {
                
                if ([temp isKindOfClass:[TeachPrivateClassViewController class]]) {
                    
                    [self.navigationController popToViewController:temp animated:YES];
                    
                    [NotificationCenter postNotificationName:@"reloadPrivateTeach" object:nil userInfo:nil];
                }
            }
            
        }
        else{
            
            [MBProgressHUD showText:[responseObject objectForKey:@"Message"]];
        }
        
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideHUDForView:self.view];
        
        [MBProgressHUD showText:@"添加失败"];
    }];
}


-(int)compareOneDay:(NSString *)oneDay withAnotherDay:(NSString *)anotherDay

{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    
    NSDate *dateA = [dateFormatter dateFromString:oneDay];
    
    NSDate *dateB = [dateFormatter dateFromString:anotherDay];
    
    NSComparisonResult result = [dateA compare:dateB];
    
    //NSLog(@"date1 : %@, date2 : %@", oneDay, anotherDay);
    
    if (result == NSOrderedDescending) {
        
        //NSLog(@"Date1  is in the future");
        
        return 1;
        
    }
    
    else if (result ==NSOrderedAscending){
        
        //NSLog(@"Date1 is in the past");
        
        return -1;
        
    }
    
    //NSLog(@"Both dates are the same");
    
    return 0;
    
}

-(NSMutableDictionary *)dataDict{
    
    if (!_dataDict) {
        
        _dataDict = [[NSMutableDictionary alloc] init];
    }
    
    return _dataDict;
}

@end
